package study.cell_game.scenes;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.CameraScene;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

import study.cell_game.activity.GameActivity;
import study.cell_game.objects.CellMap;

public class GameScene extends CameraScene{
	
	private static GameScene instance;
	public static GameScene getInstance(){
		if(instance == null){
			instance = new GameScene();
		}
		return instance;
	}
	
	private CellMap Map;
	
	private Entity mainLayer = new Entity();
	private Entity overlayLayer = new Entity();
	//private Sprite[] Cells; 
	
	public Entity getMainLayer(){
		return mainLayer;
	}
	
	public Entity getOverlayLayer(){
		return overlayLayer;
	}
	
	private GameScene() {
		super(GameActivity.mCamera);
		Map = new CellMap();
		//Cells = new Sprite[Map.getCellNum()];
	
		setBackground(new SpriteBackground(new Sprite(0, 0, GameActivity.mTextures.getGradient(), GameActivity.main.getVertexBufferObjectManager())));
		attachChild(mainLayer);
		attachChild(overlayLayer);
		mainLayer.attachChild(new Sprite(-50, -70, GameActivity.mTextures.getBackground(), GameActivity.main.getVertexBufferObjectManager()));
		mainLayer.attachChild(new Sprite(175, 10, GameActivity.mTextures.getFields(), GameActivity.main.getVertexBufferObjectManager()));
		
		for(int i=0; i<Map.getCellNum(); i++){
			mainLayer.attachChild(Map.getCell(i).getSprite());
			registerTouchArea(Map.getCell(i).getSprite());
			//Map.getCell(i).setMapCell(true);
			//unregisterTouchArea(Map.getCell(i).getSprite());
			//registerTouchArea(Map.getCell(i));
		}
	}

	@Override
	public boolean onSceneTouchEvent(TouchEvent pSceneTouchEvent) {
//		switch (pSceneTouchEvent.getAction()) {
//		case TouchEvent.ACTION_DOWN:
//			for(int i=0; i<Player.getInstance().getHand().getTokensOnHand().size(); i++)
//			{
//				if(Player.getInstance().getHand().getTokensOnHand().get(i).getPlayerColor().getX()< pSceneTouchEvent.getX()
//						&& Player.getInstance().getHand().getTokensOnHand().get(i).getPlayerColor().getY() < pSceneTouchEvent.getY()
//						&& Player.getInstance().getHand().getTokensOnHand().get(i).getPlayerColor().getX() + 60 > pSceneTouchEvent.getX()
//						&& Player.getInstance().getHand().getTokensOnHand().get(i).getPlayerColor().getY() + 70 > pSceneTouchEvent.getY())
//				{
//					Player.getInstance().getHand().getTokensOnHand().get(i).getPlayerColor().setScale((float)1.1);
//					for(int j=0; j<Player.getInstance().getHand().getTokensOnHand().size(); j++)
//					{
//						if(Player.getInstance().getHand().getTokensOnHand().get(j).getPlayerColor().isGrabbed()==true)
//						{
//							Player.getInstance().getHand().getTokensOnHand().get(j).getPlayerColor().setGrabbed(false);
//							Player.getInstance().getHand().getTokensOnHand().get(j).getPlayerColor().setScale((float)1.0);
//						}
//					}
//					Player.getInstance().getHand().getTokensOnHand().get(i).getPlayerColor().setGrabbed(true);
//					if(Player.getInstance().getHand().getTokensOnHand().get(i).getUsable()==false)
//					{
//					for (Cell cell : Player.getInstance().getAvailableCells()) {
//						cell.getSprite().attachChild(
//								new Sprite(0, 0, TokenTextures.getInstance().getFullRing(), GameActivity.main
//										.getVertexBufferObjectManager()));
//					}
//					}
//				}	
//			}
//			
//			Token token = null;
//			for(int j=0; j<Player.getInstance().getHand().getTokensOnHand().size(); j++)
//			{
//				if(Player.getInstance().getHand().getTokensOnHand().get(j).getPlayerColor().isGrabbed()==true)
//				{
//					token = Player.getInstance().getHand().getTokensOnHand().get(j);
//				}
//			}
//			/*if (token!=null)
//			{
//				for (Cell cell : Player.getInstance().getAvailableCells()) {
//					cell.getSprite().detachChildren();
//				}
//			}*/
//			break;
//		}
//		return false;//
		
		return super.onSceneTouchEvent(pSceneTouchEvent);
	}
	
	public CellMap getMap()
	{
		return Map;
	}
}
